package com.file.system.plugin.websocket;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.file.system.entity.res.BaseCode;
import com.file.system.entity.system.WebNotice;
import com.file.system.service.impl.system.WebNoticeServiceImpl;
import com.file.system.service.system.IWebNoticeService;
import com.file.system.tools.Formater;

import java.util.List;

/**
 * 查询系统所有公告
 */
public class PublicTask implements Runnable {

    // 获取WebNoticeServiceImpl实例
    private IWebNoticeService iWebNoticeService = SpringUtil.getBean(WebNoticeServiceImpl.class);
    private String shiroId;

    public PublicTask(String shiroId) {
        this.shiroId = shiroId;
    }

    @Override
    public void run() {
        // 进入系统后两分钟推送系统公告
        ThreadUtil.sleep(1000 * 60);
        // 查询所有未到期公告
        List<WebNotice> noticeList = iWebNoticeService.list(new QueryWrapper<WebNotice>()
                .ge("a.end_date", Formater.getNow())
                .eq("a.is_del", 0)
                .eq("a.dict_on_off", "0")
                .orderByDesc("a.id")
        );

        // 循环推送公告
        for (WebNotice notice : noticeList) {
            WebSocketServer.sendMessage(BaseCode.Common.MSG_PUBLIC, notice, this.shiroId);
        }
    }
}
